package com.study.aspect;

import com.alibaba.fastjson.JSON;
import com.study.annotation.MyAnnotation1;
import com.study.dto.UserDto;
import java.lang.reflect.Field;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @Author tanglingyu
 * @Date: 2021/10/12/ 19:15
 * @Description
 */
@Aspect
@Component
@Order(1)
public class MyAspect {

  @Pointcut(value = "@annotation(sb)")
  public void aspect(MyAnnotation1 sb) {

  }

  @Pointcut(value = "execution(* com.study.controller.TestController1.*(..)))")
  public void aspect2() {
    System.out.println("=====================================");
  }

  @Around(value = "aspect(sb)")
  public Object around(ProceedingJoinPoint proceedingJoinPoint,
      MyAnnotation1 sb)
      throws Throwable {
    Object[] args = proceedingJoinPoint.getArgs();
    final String[] name = sb.name();
    System.out.println(name[0]);
    System.out.println(name[1]);
    final UserDto userDto = (UserDto) args[0];
    userDto.setName("汤凌宇");
    userDto.setTestName(name[0]);
    userDto.setLocation(name[1]);
    System.out.println("aspect...");
    return proceedingJoinPoint.proceed(args);
  }

  //@Around(value = "execution(* com.study.controller.TestController1.*(..)))")
  @Around(value = "aspect2()")
  public Object as(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
    Object[] args = proceedingJoinPoint.getArgs();
    System.out.println(JSON.toJSONString(args[0]));
    return proceedingJoinPoint.proceed(args);
  }
}
